package com.uchiha.nova.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.uchiha.nova.core.model.entity.DictBizEntity;
import com.uchiha.nova.core.model.vo.DictBizVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaConstant;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.util.BeanUtil;
import com.uchiha.nova.core.tool.util.CacheUtil;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.cache.CacheConstant;
import com.uchiha.nova.mainService.cache.DictBizCache;
import com.uchiha.nova.mainService.mapper.DictBizMapper;
import com.uchiha.nova.mainService.service.IDictBizService;
import com.uchiha.nova.mainService.wrapper.DictBizWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务字典 服务实现类
 *
 * @author Uchihashiamu
 */
@Service
public class DictBizServiceImpl extends BaseServiceImpl<DictBizMapper, DictBizEntity> implements IDictBizService {
    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Override
    public DictBizEntity getValue(String code, String dictKey) {
        return this.getOne(new QueryWrapper<DictBizEntity>().eq("code",code).eq("dict_key",dictKey));
    }

    @Override
    public R submit(DictBizEntity dict) {
        LambdaQueryWrapper<DictBizEntity> lqw = Wrappers.<DictBizEntity>query().lambda().eq(DictBizEntity::getCode, dict.getCode()).eq(DictBizEntity::getDictKey, dict.getDictKey());
        Long cnt = baseMapper.selectCount((Func.isEmpty(dict.getId())) ? lqw : lqw.notIn(DictBizEntity::getId, dict.getId()));
        if (cnt > 0L) {
            R.fail("当前字典键值已存在!");
        }
        // 修改顶级字典后同步更新下属字典的编号
        if (Func.isNotEmpty(dict.getId()) && dict.getParentId().longValue() == NovaConstant.TOP_PARENT_ID) {
            DictBizEntity parent = BeanUtil.copy(DictBizCache.getById(dict.getId()),DictBizEntity.class);
            this.update(Wrappers.<DictBizEntity>update().lambda().set(DictBizEntity::getCode, dict.getCode()).eq(DictBizEntity::getCode, parent.getCode()).ne(DictBizEntity::getParentId, NovaConstant.TOP_PARENT_ID));
        }
        if (Func.isEmpty(dict.getParentId())) {
            dict.setParentId(NovaConstant.TOP_PARENT_ID);
        }
        dict.setDeleted(NovaConstant.DB_NOT_DELETED);
        CacheUtil.clear(CacheConstant.BIZ_CACHE,false);
        return R.status(saveOrUpdate(dict));
    }

    @Override
    public List<DictBizVO> childList(DictBizEntity dictBiz) {
        if (dictBiz.getParentId().compareTo(0L)==-1) {
            return new ArrayList<>();
        }
        Long parentId = dictBiz.getParentId();
        DictBizEntity parentDict = this.baseMapper.selectById(parentId);
        dictBiz.setParentId(null);
        List<DictBizEntity> list = this.list(Condition.getQueryWrapper(dictBiz).lambda().ne(DictBizEntity::getId, parentId).eq(DictBizEntity::getCode, parentDict.getCode()));
        return DictBizWrapper.build().listVO(list);
    }
}
